package cn.globalph.housekeeper.ui.task.check

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import cn.globalph.housekeeper.BuildConfig
import cn.globalph.housekeeper.Event
import cn.globalph.housekeeper.R
import cn.globalph.housekeeper.data.model.RoomInfoModel
import cn.globalph.housekeeper.data.params.RoomInfoParam
import cn.globalph.housekeeper.ui.BaseViewModel
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.lang.Exception

class CheckCardViewModel (
    private val repository: CheckCardRepository
):BaseViewModel(){
    private val _checkIcon = MutableLiveData<Int>().apply { value = R.drawable.ic_check_un }
    var checkIcon:LiveData<Int> = _checkIcon

    private val _roomInfo = MutableLiveData<RoomInfoModel>()
    var roomInfo:LiveData<RoomInfoModel> = _roomInfo

    var taskId:String?=null
    var attentions:String?=null

    val hasAttentions:Boolean
    get() {
        return !attentions.isNullOrEmpty()
    }

    fun start(){
        taskId?.let {
            viewModelScope.launch {
                try{
                    val m1 = async { repository.getRoomInfo(it) }
                    val m2 = async { repository.getCheckInfo(it) }
                    val model1 = m1.await()
                    val model2 = m2.await()

                    if(model1.isSuccessful() && model1.data != null){
                        _roomInfo.value = model1.data
                    }

                    if(model2.isSuccessful() && model2.data!=null){
                        _checkIcon.value = R.drawable.ic_check
                    }

                    if(model1.isCertFail() || model2.isCertFail()){
                        _tokenEvent.value = Event(true)
                        return@launch
                    }

                    if(!model1.isSuccessful()){
                        _toastEvent.value = Event(model1.message)
                    }
                    if(!model2.isSuccessful()){
                        _toastEvent.value = Event(model2.message)
                    }
                }catch (e:Exception){
                    if(BuildConfig.DEBUG)e.printStackTrace()
                    _toastEvent.value = Event("网络异常，请重试")
                }
            }
        }
    }

    fun checkClick(){
        if(_checkIcon.value == R.drawable.ic_check_un){
            _checkIcon.value = R.drawable.ic_check
        }else{
            _checkIcon.value = R.drawable.ic_check_un
        }
    }

    fun uploadRoomInfo(n1:Int,n2:Int,n3:Int,n4:Int,n5:Int){
        if(taskId != null ){
            viewModelScope.launch {
                try{
                    var roomId:String? = null
                    if(_roomInfo.value != null){
                        roomId = _roomInfo.value!!.id
                    }
                    val model = repository.uploadRoomInfo(
                        taskId!!,
                        RoomInfoParam(
                            roomId,n1.toString(),n2.toString(),
                            n3.toString(),n4.toString(),n5.toString()))
                    when{
                        model.isSuccessful()->{
                            _toastEvent.value = Event("更新成功")
                        }
                        model.isCertFail()->_tokenEvent.value = Event(true)
                        else->_toastEvent.value = Event(model.message)
                    }
                }catch (e:Exception){
                    if(BuildConfig.DEBUG) e.printStackTrace()
                    _toastEvent.value = Event("网络异常，请重试")
                }
            }
        }
    }

    fun uploadCheck(){
        if (_checkIcon.value == R.drawable.ic_check_un){
            _toastEvent.value = Event("未勾选全部检查")
            return
        }
        taskId?.let {
            viewModelScope.launch {
                try {
                    val model = repository.uploadCheck(it)
                    when{
                        model.isSuccessful()->_toastEvent.value = Event("提交成功")
                        model.isCertFail()->_tokenEvent.value = Event(true)
                        else->_toastEvent.value = Event(model.message)
                    }
                }catch (e:Exception){
                    if(BuildConfig.DEBUG)e.printStackTrace()
                    _toastEvent.value = Event("网络异常,请重试")
                }
            }
        }
    }


}